package com.rory.gooday.consumption;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Page;
import com.rory.gooday.common.R;
import com.rory.gooday.common.model.Blog;
import com.rory.gooday.common.model.Consumption;
import com.rory.gooday.common.model.ConsumptionDate;

@Path("/consumption")
public class ConsumptionController extends Controller {
	
	@Inject
	ConsumptionService service;
	
	public void index() {
		render("consumption.html");
	}

	public void dateData() {
		Page<ConsumptionDate> a =  service.datePaginate(getParaToInt("page", 1), getParaToInt("limit", 10));
		renderJson(R.ok(a));
	}

	public void detailListPage() {
		int cid = getParaToInt();
		Page<Consumption> a =  service.consumptionPaginate(cid,getParaToInt("page", 1), getParaToInt("limit", 10));
		set("consumption_id", cid);
		render("list.html");
	}
	public void detailListData() {
		int cid = getParaToInt("consumption_id");
		Page<Consumption> a =  service.consumptionPaginate(cid,getParaToInt("page", 1), getParaToInt("limit", 10));
		renderJson(R.ok(a));
	}

	public void exp() {
		int consumption_id = getParaToInt(0);
		int type = getParaToInt(1);
		setAttr("cols", service.getConsumptionCols(type));
		setAttr("consumption_id", consumption_id);
		render("exp.html");
	}

	public void addDetail() {
		String money = getPara("money");
		Consumption consumption = getBean(Consumption.class);
		consumption.setMoney(new java.math.BigDecimal(money));
		service.createConsumption(consumption);
		renderJson(R.ok(consumption.getId()));
	}
	public void add() {
	}
	
	public void save() {
		Consumption b = getBean(Consumption.class);
		renderJson(R.ok(1));
	}
	
	public void edit() {
		Consumption a = service.findById(getParaToInt());
		setAttr("blog", a);
	}
	
	public void update() {
		getBean(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		service.deleteById(getParaToInt());
		redirect("/blog");
	}
}


